package com.business.api.official.toursism.websitenews.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.api.official.toursism.websitenews.dto.WebsiteNewsApiDTO;
import com.business.api.official.toursism.websitenews.from.WebsiteNewsApiSaveFrom;
import com.business.api.official.toursism.websitenews.from.WebsiteNewsApiUpdateFrom;
import com.business.api.official.toursism.websitenews.manager.WebsiteNewsApiManager;
import com.business.api.official.toursism.websitenews.query.WebsiteNewsApiPageQuery;
import com.business.api.official.toursism.websitenews.query.WebsiteNewsApiQuery;
import com.business.api.official.toursism.websitenews.service.WebsiteNewsApiService;
import com.business.api.official.toursism.websitenews.vo.WebsiteNewsApiBriefPageVO;
import com.business.api.official.toursism.websitenews.vo.WebsiteNewsApiDetailListVO;
import com.business.api.official.toursism.websitenews.vo.WebsiteNewsApiDetailPageVO;
import com.business.api.official.toursism.websitenews.vo.data.WebsiteNewsApiBriefData;
import com.business.api.official.toursism.websitenews.vo.data.WebsiteNewsApiDetailData;
import com.business.system.official.toursism.websitenews.entity.WebsiteNews;
import com.business.system.official.toursism.websitenews.service.WebsiteNewsService;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.dto.SeedMessageDTO;
import com.seed.core.pojo.service.impl.SeedApiServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 乌镇新闻
 * @author CodeGenerator
 * @date   2025-01-25
 * @version V1.0
 */
@Slf4j
@Service("websiteNewsApiService")
@Transactional(rollbackFor = ServiceException.class)
public class WebsiteNewsApiServiceImpl extends SeedApiServiceImpl<WebsiteNewsApiManager, WebsiteNews> implements WebsiteNewsApiService {

    @Autowired
    private WebsiteNewsService systemService;


    /**
     * 持久化
     *
     * @param websiteNewsFrom 需要保存的对象
     * @return id，如果保存失败返回空字符串
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteNewsApiDTO save(WebsiteNewsApiSaveFrom websiteNewsFrom) throws ServiceException {
        WebsiteNewsApiDTO apiDTO = new WebsiteNewsApiDTO();
        WebsiteNews websiteNews = new WebsiteNews();
        try {
            BeanUtils.copyProperties(websiteNewsFrom, websiteNews, "id");
        } catch (Exception e) {
            throw new ServiceException(e, websiteNewsFrom);
        }
        SeedMessageDTO baseSeedDTO = this.save(websiteNews);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        apiDTO.setWebsiteNews(websiteNews);
        return apiDTO;
    }

    /**
     * 部分更新
     *
     * @param websiteNewsFrom 需要更新的对象
     * @return 是否成功
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteNewsApiDTO updateSelectiveById(WebsiteNewsApiUpdateFrom websiteNewsFrom) throws ServiceException {
        WebsiteNewsApiDTO apiDTO = new WebsiteNewsApiDTO();
        if (StringUtils.isEmpty(websiteNewsFrom.getId())) {
            apiDTO.setCode("A0410");
            apiDTO.setMessage("更新主键为空");
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        WebsiteNews websiteNews = new WebsiteNews();
        try {
            BeanUtils.copyProperties(websiteNewsFrom, websiteNews);
        } catch (Exception e) {
            throw new ServiceException(e, websiteNewsFrom);
        }
        SeedMessageDTO baseSeedDTO = this.updateById(websiteNews);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        return apiDTO;
    }

    /**
     * 获取全部
     *
     * @param query 查询条件
     * @return 详情VOList
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteNewsApiDetailListVO listAll(WebsiteNewsApiQuery query) throws ServiceException {
        WebsiteNews queryEntity = new WebsiteNews();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteNews> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        List<WebsiteNews> list = this.list(lambdaQueryWrapper);
        List<WebsiteNewsApiDetailData> detailVOList;
        try {
            detailVOList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteNewsApiDetailListVO(detailVOList);
    }

    /**
     * 简略对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的简略对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteNewsApiBriefPageVO getBriefPageVO(WebsiteNewsApiPageQuery query) throws ServiceException {
        Page<WebsiteNews> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteNews queryEntity = new WebsiteNews();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteNews> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteNews> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteNews> list = pageData.getRecords();
        List<WebsiteNewsApiBriefData> voList;
        try {
            voList = baseManager.listConvertApiBriefData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteNewsApiBriefPageVO(page, voList);
    }

    /**
     * 详细对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的详细对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteNewsApiDetailPageVO getDetailPageVO(WebsiteNewsApiPageQuery query) throws ServiceException {
        Page<WebsiteNews> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteNews queryEntity = new WebsiteNews();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteNews> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteNews> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteNews> list = pageData.getRecords();
        List<WebsiteNewsApiDetailData> voList;
        try {
            voList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteNewsApiDetailPageVO(page, voList);
    }

    /**
     * 根据标题查询文章
     *
     * @param title 标题名称
     * @return 文章详情
     */
    @Override
    public WebsiteNewsApiDetailListVO listByTitle(String title) throws ServiceException {
        List<WebsiteNewsApiDetailData> list = new ArrayList<>();

        LambdaQueryWrapper<WebsiteNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WebsiteNews::getIsEnbale, 1)
                .in(WebsiteNews::getType, 3, 4)
                .like(WebsiteNews::getNewsTitle, title);
        List<WebsiteNews> websiteNewsList = this.list(queryWrapper);
        try {
            list = baseManager.listConvertApiTitleData(websiteNewsList);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteNewsApiDetailListVO(list);
    }

}